//
//  MSAlertFightView.m
//  MagicSword
//
//  Created by Netease on 15/11/23.
//  Copyright © 2015年 Jonear. All rights reserved.
//

#import "MSAlertFightView.h"
#import "MSEnemyInfo.h"
#import "MSWeaponNode.h"
#import "MSProgressBar.h"

@implementation MSAlertFightView {
    SKSpriteNode *_enemyNode;
    SKLabelNode  *_nameLabel;
    SKLabelNode  *_attackLabel;
    SKSpriteNode *_detailBackgroud;
    SKLabelNode  *_detailLabel;
    MSEnemyInfo  *_enemyInfo;
    MSEnemySprite *_enemySprite;
    MSProgressBar *_prgressBar;
}

- (id)init {
    self = [super init];
    if (self) {
        [self setButtonHidden:YES];
    }
    
    return self;
}

- (void)setEnemySprite:(MSEnemySprite *)sprite {
    SKTextureAtlas *atlas = [SKTextureAtlas atlasNamed:@"enemy_thrumb"];
    _enemySprite = sprite;
    MSEnemyInfo *info = [sprite enemyInfo];
    _enemyInfo = info;
    
    if (!_enemyNode) {
        _enemyNode = [[SKSpriteNode alloc] init];
        _enemyNode.position = CGPointMake(-60, 35);
        _enemyNode.zPosition = 11;
        _enemyNode.size = CGSizeMake(70, 70);
        [self addChild:_enemyNode];
    }
    [_enemyNode setTexture:[atlas textureNamed:info.mapImage]];
    
    if (!_prgressBar) {
        _prgressBar = [[MSProgressBar alloc] initWithSize:CGSizeMake(80, 16)];
        _prgressBar.titleLabelNode.text = [NSString stringWithFormat:@"%d/%d", info.HP, info.MaxHP];
        _prgressBar.titleLabelNode.fontSize = 11.;
        _prgressBar.position = CGPointMake(-65, 5);
        _prgressBar.zPosition = 12;
        [self addChild:_prgressBar];
    }
    _prgressBar.progress = info.HP/(float)info.MaxHP;
    
    if (!_nameLabel) {
        _nameLabel = [SKLabelNode labelNodeWithFontNamed:@"Helvetica"];
        _nameLabel.position = CGPointMake(35, 35);
        _nameLabel.zPosition = 11;
        _nameLabel.fontSize = 20;
        _nameLabel.fontColor = UIColorFromRGB(0x2f2f30);
        [self addChild:_nameLabel];
    }
    [_nameLabel setText:[NSString stringWithFormat:@"%@", info.name]];
    
    if (!_attackLabel) {
        _attackLabel = [SKLabelNode labelNodeWithFontNamed:@"Helvetica"];
        _attackLabel.position = CGPointMake(35, 8);
        _attackLabel.zPosition = 11;
        _attackLabel.fontSize = 13;
        _attackLabel.fontColor = UIColorFromRGB(0x2f2f30);
        [self addChild:_attackLabel];
    }
    [_attackLabel setText:[NSString stringWithFormat:@"攻击: %d", info.Attack]];
    
    if (!_detailBackgroud) {
        _detailBackgroud = [SKSpriteNode spriteNodeWithImageNamed:@"main_alert_detail"];
        _detailBackgroud.position = CGPointMake(0, -20);
        _detailBackgroud.zPosition = 11;
        _detailBackgroud.size = CGSizeMake(213, 25);
        [self addChild:_detailBackgroud];
    }
    
    if (!_detailLabel) {
        _detailLabel = [SKLabelNode labelNodeWithFontNamed:@"Helvetica"];
        _detailLabel.position = CGPointMake(0, -25);
        _detailLabel.zPosition = 12;
        _detailLabel.fontSize = 13;
        _detailLabel.fontColor = UIColorFromRGB(0xf25062);
        [self addChild:_detailLabel];
    }
    if (info.detailMsg.length > 0) {
        _detailLabel.fontColor = UIColorFromRGB(0xf25062);
        [_detailLabel setText:info.detailMsg];
    } else {
        _detailLabel.fontColor = [UIColor grayColor];
        [_detailLabel setText:@"无特殊属性"];
    }
}

- (void)setHeroWeaponInfo:(NSArray *)weaponArray {
    for (int i=0; i<3; i++) {
        MSWeaponNode *weaponSpite = nil;
        if (weaponArray.count > i) {
            weaponSpite = [[MSWeaponNode alloc] initWithWeaponInfo:weaponArray[i]];
        } else {
            weaponSpite = [[MSWeaponNode alloc] init];
        }
        
        weaponSpite.position = CGPointMake(-60+i*60, -60);
        weaponSpite.size = CGSizeMake(30, 30);
        weaponSpite.zPosition = 12;
        [weaponSpite setShapeNodeHidden:NO];
        weaponSpite.name = NodeName_AttackWithWeapon;
        [self addChild:weaponSpite];
    }
}

@end
